package com.gujia.controller;

import com.gujia.model.Comment;
import com.gujia.model.Question;
import com.gujia.model.User;
import com.gujia.model.ViewObject;
import com.gujia.service.CommentService;
import com.gujia.service.LikeService;
import com.gujia.service.QuestionService;
import com.gujia.service.UserService;
import com.gujia.util.AnswerUtil;
import com.gujia.util.EntityType;
import com.gujia.util.LoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by GuJia on 2017/6/28.
 */
@Controller
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private UserService userService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private LikeService likeService;

    @RequestMapping(path={"/question/add"},method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public String addQuestion(Model model, @RequestParam("title") String title, @RequestParam("content") String content){


        try{

            Question question = new Question();

            question.setTitle(title);
            question.setContent(content);
            question.setCreatedDate(new Date());
            question.setCommentCount(0);

            if(LoginUtil.getUser() == null){
                return AnswerUtil.getJSONString(999);
            }else{
                question.setUserId(LoginUtil.getUserId());
            }

            if(questionService.addQuestion(question)>0){
                return AnswerUtil.getJSONString(0);
            }
        }catch (Exception e){
            e.printStackTrace();

        }

        return AnswerUtil.getJSONString(1,"Add question failed!");



    }


    @RequestMapping(path={"/question/{qid}"},method = {RequestMethod.GET,RequestMethod.POST})
    public String addQuestion(Model model, @PathVariable("qid") int qid){


        try{

            Question question = questionService.selectById(qid);
            User user = userService.getUser(question.getUserId());
            model.addAttribute("question",question);
            model.addAttribute("user",user);
            List<Comment> commentList = commentService.selectByEntity(question.getId(), EntityType.ENTITY_QUESTION);
            List<ViewObject> comments = new ArrayList<>();
            for(Comment comment:commentList){
                ViewObject vo = new ViewObject();
                vo.set("user",userService.getUser(comment.getUserId()));
                vo.set("comment",comment);
                if(LoginUtil.getUser() == null){
                    vo.set("liked",0);
                }else{
                    vo.set("liked",likeService.getLikeStatus(LoginUtil.getUserId(),EntityType.ENTITY_COMMENT,comment.getId()));
                    //vo.set("liked",0);
                }
                vo.set("likeCount",likeService.getLikeCount(EntityType.ENTITY_COMMENT,comment.getId()));

               // vo.set("likeCount",1);
                comments.add(vo);
            }
            model.addAttribute("comments",comments);

        }catch (Exception e){
            e.printStackTrace();

        }

        return "detail";



    }
}
